package ias.springnote.rest;

import static org.hamcrest.Matchers.greaterThan;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

import jdave.Specification;
import jdave.junit4.JDaveRunner;

import org.hamcrest.Matchers;
import org.junit.runner.RunWith;

import com.openmaru.api.ErrorType;
import com.openmaru.api.ErrorsType;
import com.springnote.api.AttachmentType;
import com.springnote.api.PageType;
import com.springnote.api.PagesType;

/**
 *
 * @author ias
 */
@RunWith(JDaveRunner.class)

public class RestClientSpec extends Specification<RestClient> {

    public class SimpleClient {
      RestClient client;
      public RestClient create() {
        client = new RestClient();
        Properties properties = new Properties();
        try {
			properties.loadFromXML(new FileInputStream("springnote.xml"));
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String userId = properties.getProperty("userId");
        String userKey = properties.getProperty("userKey");
        String appKey = properties.getProperty("appKey");
        client.setAuthInfo(userId, userKey, appKey);
        return client;
      }
      
      /*
      public void callRight() throws ResourceException {
        PagesType pages = client.getPages();
        for (PageType page : pages.getPage()) {
			System.out.println(page);
		}
        specify(pages.getPage().size(), greaterThan(0));
      }
      
      public void checkErrors() {
          try {
			PageType page = client.getPage(37);
		} catch (ResourceException e) {
			ErrorsType errors = e.getErrors();
			for (ErrorType error : errors.getError()) {
				System.out.println(error.getTitle());
				System.out.println(error.getDescription());
			}
		}
      }
      
      public void getAttachementStream() throws ResourceException {
    	  AttachmentType attachment = client.getAttachment(393390, 190659);
    	  specify(attachment, Matchers.notNullValue());
    	  System.out.println(attachment.getTitle());
    	  InputStream attachmentStream = client.getAttachmentStream(393390, 190659);
    	  specify(attachmentStream, Matchers.notNullValue());
      }
      public void putAttachementStream() throws ResourceException {
          String sample = "test";
    	  AttachmentType putAttachmentStream = client.putAttachmentStream(512602, sample.getBytes(), "test.txt");
          System.out.println(putAttachmentStream);
      }
*/
      
      public void getPagesByTag() throws ResourceException {
          client.setDomain("webcode");
          PagesType pages = client.getPagesByTag("transform");
          System.out.println(pages.getPage().size());
      }
    }

}
